<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Setting extends CI_Controller {

    function __construct() {
        parent::__construct();

        /* Standard Libraries of codeigniter are required */
        $this->load->database();
        $this->load->helper('url');
        /* ------------------ */

        $this->load->library('ExCRUD');
    }

    function harga() {
        $crud = new ExCRUD();
        $crud->standartCheck();


        // 1. set table & relation        
        $crud->set_table('harga_produk');
        $crud->set_primary_key("id_produk");
        $crud->order_by('id_produk', 'desc');

        // 2. set table display                        
        $crud->set_delete(false);
        $crud->set_add(false);
        $crud->display_as("id_produk", "Produk");
        $crud->display_as("date_changed", "Tanggal Diubah");

        // 3. set tampilan kolom        
        $list_column_views = array('id_produk', 'harga_beli', 'harga_jual', 'date_changed');
        $crud->columns($list_column_views);
        $crud->set_field_hidden($crud, array('date_submit'));
        $crud->set_field_readonly($crud, array('date_changed', 'id_produk'));

        // render it dude
        $output = $crud->render();
        $this->load->view('template/view_admin', $output);
    }
    
    function data_download() {
        $crud = new ExCRUD();
        $crud->standartCheck();


        // 1. set table & relation        
        $crud->set_table('data_download');
        $crud->set_primary_key("id_download");
        $crud->order_by('nama', 'asc');

        // 2. set table display
        $crud->display_as("is_active", "Tampilkan Download");        
        $crud->field_type('is_active', 'dropdown', array(1 => "Tampilkan", 0 => "Sembunyikan"));
        
        // 3. set tampilan kolom        
        
        // render it dude
        $output = $crud->render();
        $this->load->view('template/view_admin', $output);
    }

    function tour($is_special = 0) {
        $is_special = intval($is_special);
        $crud = new ExCRUD();
        $crud->standartCheck();

        // 1. set table & relation        
        $crud->set_table('data_tour');
        $crud->set_primary_key("id_tour");        
        if ($is_special === 0 && $crud->getState() == 'list'){
            $this->session->set_userdata("special_tour","");
        } else if ($is_special === 1 && $crud->getState() == 'list'){
            $this->session->set_userdata("special_tour","special");
        }        
        if ($is_special === 1 || $this->session->userdata("special_tour") === "special"){                                                            
            $crud->where("is_special_tour", 1);            
            $crud->set_field_hidden($crud, array("is_publish_for_pooling","requirement_poin"));
        } else {            
            $this->session->set_userdata("special_tour","");
            $crud->where("is_special_tour", 0);              
            $crud->set_field_hidden($crud, array("harga_rupiah"));            
        }
        
        $crud->field_type('is_publish_for_pooling', 'dropdown', array(1 => "Tampilkan", 0 => "Sembunyikan"));
        $crud->field_type('is_domestik_tour', 'dropdown', array(1 => "Domestik", 0 => "Internasional"));   
        $crud->order_by('id_tour', 'desc');

        // 2. set table display        
        $crud->display_as("is_domestik_tour", "Jenis Tour");
        $crud->display_as("is_publish", "Tampilkan Ke Member Area");
        $crud->display_as("urutan", "Urutan Tampil");
        $crud->display_as("is_publish_for_pooling", "Tampilkan Ke Data Pooling");
        $crud->display_as("is_special_tour", "Tampilkan Sebagai Tour Khusus");
        $crud->add_action('Edit Foto Tour', base_url('static/images/4.png'), 'setting/foto_tour');
        $crud->add_action('Edit Itenary', base_url('static/images/2.png'), 'setting/foto_itenary');
        $crud->columns(array("id_tour","nama","requirement_poin","is_publish","urutan","is_publish_for_pooling", "is_special_tour"));
        
        // 3. set tampilan kolom        
        $crud->field_type('is_publish', 'dropdown', array(1 => "Tampilkan", 0 => "Sembunyikan"));        
        $crud->field_type('is_special_tour', 'dropdown', array(1 => "Iya", 0 => "Tidak"));
        
        $crud->callback_after_insert(array($this, 'tour_after_insert'));
        $crud->callback_after_delete(array($this, 'tour_after_delete'));

        // render it dude
        $output = $crud->render();
        $this->load->view('template/view_admin', $output);
    }
    
    function foto_itenary($id_tour){
        $this->load->model("model_tour");
        $crud = new ExCRUD();
        $crud->standartCheck();
        $data['listFoto'] = $this->model_tour->getFotoItenary($id_tour);
        $data['id'] = $id_tour;
        $this->session->set_userdata("id_tour", $id_tour);
        $this->load->view('template/itenary_detil', $data);
    }
    
    function foto_itenary_tambah($id_tour) {
        $this->load->model("model_tour");
        $this->model_tour->tambahFotoItenary($id_tour);        
        redirect(base_url("index.php/setting/foto_itenary/".$id_tour));
    }
    
    function foto_itenary_hapus($id_tour, $id_foto) {
        $this->load->model("model_tour");
        $this->model_tour->hapusFotoItenary($id_foto);
        redirect(base_url("index.php/setting/foto_itenary/".$id_tour));
    }
    
    function foto_itenary_upload($id_tour, $id_foto) {
        $this->load->model("model_tour");
        global $g_base_dir_foto;
               
        $config = array();
        $config['upload_path'] = $g_base_dir_foto."/".$id_tour;
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size']	= '0';
        $pathName = $id_tour."_".$id_foto;
        $config['file_name'] = $pathName;
        $config['overwrite'] = true;
                
        $this->load->library('upload', $config);

        if ($this->upload->do_upload()) {            
            $data = $this->upload->data();
            $this->model_tour->updatePathFotoItenary($id_foto, "assets/img/tour/".$id_tour."/".$pathName.$data['file_ext']);
            $data['success'] = "Foto ".$data['client_name']." berhasil diupload.";            
        }        
        redirect(base_url("index.php/setting/foto_itenary/".$id_tour));
    }
    
    function foto_itenary_info() {
        $this->load->library("session");
        $crud = new ExCRUD();                
        
        $crud->standartCheck();

        // 1. set table & relation        
        $crud->set_table('itenary_tour');
        $crud->set_primary_key("id_itenary");        
        $crud->order_by('id_itenary', 'desc');

        // 2. set table display        
        $crud->display_as("subjek", "Judul Itenary");
        $crud->display_as("itenary_konten", "Isi Itenary");
        $crud->display_as("urutan", "Nomor Urut Tampil");
        
        // 3. set tampilan kolom                
        $crud->set_field_hidden($crud, array("path_foto","id_tour"));
        
        // render it dude
        $output = $crud->render();
        $this->load->view('template/view_admin', $output);
    }   
    
    function foto_tour($id_tour){
        $this->load->model("model_tour");
        $crud = new ExCRUD();
        $crud->standartCheck();
        $data['listFoto'] = $this->model_tour->getFoto($id_tour);
        $data['id'] = $id_tour;
        $this->session->set_userdata("id_tour",$id_tour);
        $this->load->view('template/tour_detil', $data);
    }
    
    function foto_tour_tambah($id_tour) {
        $this->load->model("model_tour");
        $this->model_tour->tambahFoto($id_tour);        
        redirect(base_url("index.php/setting/foto_tour/".$id_tour));
    }
    
    function foto_tour_hapus($id_tour, $id_foto) {
        $this->load->model("model_tour");
        $this->model_tour->hapusFoto($id_foto);
        redirect(base_url("index.php/setting/foto_tour/".$id_tour));
    }
    
    function foto_tour_upload($id_tour, $id_foto) {
        $this->load->model("model_tour");
        global $g_base_dir_foto;
               
        $config = array();
        $config['upload_path'] = $g_base_dir_foto."/".$id_tour;
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size']	= '0';
        $pathName = $id_tour."_".$id_foto;
        $config['file_name'] = $pathName;
        $config['overwrite'] = true;
                
        $this->load->library('upload', $config);

        if ($this->upload->do_upload()) {            
            $data = $this->upload->data();
            $this->model_tour->updatePathFoto($id_foto, "assets/img/tour/".$id_tour."/".$pathName.$data['file_ext']);
            $data['success'] = "Foto ".$data['client_name']." berhasil diupload.";            
        }        
        redirect(base_url("index.php/setting/foto_tour/".$id_tour));
    }
    
    function foto_tour_info() {
        $this->load->library("session");
        $crud = new ExCRUD();                
        
        $crud->standartCheck();

        // 1. set table & relation        
        $crud->set_table('foto_tour');
        $crud->set_primary_key("id_foto");        
        $crud->order_by('id_tour', 'desc');

        // 2. set table display        
        $crud->display_as("is_foto_utama", "Tampilkan Sebagai Foto Utama");
        $crud->display_as("hint", "Keterangan");
        $crud->display_as("urutan", "Nomor Urut Tampil");
        
        // 3. set tampilan kolom        
        $crud->field_type('is_foto_utama', 'dropdown', array(1 => "Tampilkan", 0 => "Tidak"));
        $crud->set_field_hidden($crud, array("path_foto","id_tour"));
        
        // render it dude
        $output = $crud->render();
        $this->load->view('template/view_admin', $output);
    }

    function tour_after_insert($post_array, $primary_key) {
        global $g_base_dir_foto;                
        mkdir($g_base_dir_foto . "/". $primary_key, 0777);        
    }

    function tour_after_delete($primary_key) {
        global $g_base_dir_foto;
        $base_dir_name = $g_base_dir_foto."/".$primary_key;
        $files = glob($base_dir_name); // get all file names
        foreach ($files as $file) { // iterate files
            if (is_file($file))
                unlink($file); // delete file
        }
        if (file_exists($base_dir_name))
            rmdir($base_dir_name);
    }
    
    function history_data_tour($do_reset = false){
        $this->load->model("model_tour");
        if ($do_reset === "do_reset"){            
            $this->model_tour->reset_data_tour();
            echo "<H1>Data Tour Berhasil Direset</H1>";
        }
                
        $crud = new ExCRUD();
        $crud->standartCheck();

        //$crud->set_theme("datatables");
        // 1. set table & relation        
        $crud->set_table('v_hasil_pooling_history');
        $crud->set_primary_key("id_tour");
        $crud->order_by('jumlah_pemilih', 'desc');
        $crud->set_add(false);
        $crud->set_edit(false);
        $crud->set_delete(false);
        
        // render it dude
        $output = $crud->render();
        $this->load->view('template/view_admin', $output);
    }
    
        
    function data_seminar() {
        $crud = new ExCRUD();
        $crud->standartCheck();

        // 1. set table & relation        
        $crud->set_table('data_seminar');
        $crud->set_primary_key("id_seminar");
        $crud->order_by('urutan', 'asc');

        // 2. set table display
        $crud->field_type("foto", "hidden");
        $crud->display_as("is_publish", "Publikasi");        
        $crud->field_type('is_publish', 'dropdown', array(1 => "Tampilkan", 0 => "Sembunyikan"));
        $crud->add_action('Edit Foto Seminar', base_url('static/images/icon-photo.png'), 'setting/foto_seminar');
        
        // 3. set tampilan kolom        
        
        // render it dude
        $output = $crud->render();
        $this->load->view('template/view_admin', $output);
    }
    
    function foto_seminar($id_seminar){
        $this->load->model("model_request");
        global $g_base_url_foto;
        
        $data['row'] = $this->model_request->getFotoSeminar($id_seminar);
        $data['id_seminar'] = $id_seminar;
        $data["g_base_url_foto"] = $g_base_url_foto;
        
        $this->load->view('template/seminar_upload', $data);
    }
    
    function foto_seminar_upload($id_seminar) {
        $this->load->model("model_request");
        global $g_base_home;
               
        $config = array();
        $config['upload_path'] = $g_base_home."/assets/img/seminar/";
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size']	= '0';
        $config['file_name'] = $id_seminar;
        $config['overwrite'] = true;
                
        $this->load->library('upload', $config);

        if ($this->upload->do_upload()) {            
            $data = $this->upload->data();
            $this->model_request->updatePathFoto($id_seminar, "assets/img/seminar/".$id_seminar.$data['file_ext']);
            
            $data['success'] = "Foto ".$data['client_name']." berhasil diupload.";            
        }  
//        die($this->upload->display_errors()." ".$g_base_home."/assets/img/seminar/");
        
        redirect("setting/foto_seminar/".$id_seminar);
    }
    
    function adjust_poin(){
        $crud = new ExCRUD();
        $crud->standartCheck();        
        $crud->set_table('data_adjust_poin');
        $crud->set_primary_key("id");        
        $crud->set_relation('id_member', 'member', '{alias_member} ({nickname})');
        $crud->callback_after_insert(array($this, 'adjust_poin_after_insert'));
        $list_column_ro = array('tanggal_submit', 'user_submit', 'alias_member');
        $crud->set_field_readonly($crud, $list_column_ro);
        
        $crud->order_by('id', 'desc');
        $crud->set_add(true);
        $crud->set_edit(false);
        $crud->set_delete(false);
        $output = $crud->render();
        $this->load->view('template/view_admin', $output);
    }
    
    function adjust_poin_after_insert($post_array, $primary_key){
        $this->load->model("model_transaksi");               
        $this->load->model("model_setting");               
        $nilai_perubahan = intval($post_array["penambahan_poin"]) - intval($post_array["pengurangan_poin"]);
        if ($nilai_perubahan < 0){
            $this->model_transaksi->add_poin($post_array["id_member"], $primary_key, "Adjust Poin", 0, abs($nilai_perubahan));
        } else if ($nilai_perubahan > 0) {
            $this->model_transaksi->add_poin($post_array["id_member"], $primary_key, "Adjust Poin", $nilai_perubahan);
        }
        $this->model_setting->update_data_adjust($post_array["id_member"], $primary_key);
    }
}

/* End of file main.php */
/* Location: ./application/controllers/main.php */